{% extends "layout/manage.html" %}

{% block css %}
    <style>
        .project {
            margin-top: 10px;
        }

        .btn-primary {
            font-size: 20px;
        }

        .panel-body {
            padding: 0;
            display: flex;
            flex-direction: row;
            justify-content: left;
            align-items: flex-start;
            flex-wrap: wrap;
        }

        .panel-body > .item {
            border-radius: 6px;
            width: 228px;
            border: 1px solid #dddddd;
            margin: 20px 10px;

        }

        .panel-body > .item:hover {
            border: 1px solid #f0ad4e;
        }

        .panel-body > .item > .title {
            height: 104px;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            border-top-left-radius: 6px;
            border-top-right-radius: 6px;
            font-size: 15px;
            text-decoration: none;
        }

        .panel-body > .item > .info {
            padding: 10px 10px;

            display: flex;
            justify-content: space-between;

            border-bottom-left-radius: 6px;
            border-bottom-right-radius: 6px;
            color: #8c8c8c;

        }

        .panel-body > .item > .info a {
            text-decoration: none;
        }

        .panel-body > .item > .info .fa-star {
            font-size: 18px;
        }

        .color-radio label {
            margin-left: 0;
            padding-left: 0;
        }

        .color-radio input[type="radio"] {
            display: none;
        }

        .color-radio input[type="radio"] + .cycle {
            display: inline-block;
            height: 25px;
            width: 25px;
            border-radius: 50%;
            border: 2px solid #dddddd;
        }

        .color-radio input[type="radio"]:checked + .cycle {
            border: 2px solid black;
        }

    </style>
{% endblock %}

{% block content %}

    <div class="container-fluid project">
        {#  新建项目按钮  #}
        <div style="margin: 10px 0;">
            <a class="btn btn-primary" data-toggle="modal" data-target="#addProjectModal">
                <i class="fa fa-plus-circle" aria-hidden="true"></i>
                新建项目
            </a>
        </div>

        {#  项目面板  #}
        {#  星标项目  #}
        {% if project_dict.star %}
            <div class="panel panel-default">
            <div class="panel-heading"><i class="fa fa-star" aria-hidden="true"></i>&nbsp; 星标项目</div>
            <div class="panel-body">

            {% for project in project_dict.star %}
                <div class="item">
                    <a href="{% url 'web:dashboard' project.value.id %}" class="title"
                       style="background-color: {{ project.value.get_color_display }}"> {{ project.value.name }}
                    </a>
                    <div class="info">
                        <div>
                            <a href="{% url 'web:project_remove_star' project_type=project.type project_id=project.value.id %}">
                                <i class="fa fa-star" aria-hidden="true" style="color: #f0ad4e;"></i>
                            </a>
                            <span>{{ project.value.creator.username }}</span>
                        </div>
                        <div>
                            <i class="fa fa-user-o" aria-hidden="true"></i>
                            <span>{{ project.value.join_count }}</span>
                        </div>

                    </div>
                </div>
            {% endfor %}
        {% endif %}
        </div>
        </div>
        {#  我创建的项目  #}
        {% if project_dict.my %}
            <div class="panel panel-default">
                <div class="panel-heading"><i class="fa fa-list" aria-hidden="true"></i>&nbsp; 我创建的</div>
                <div class="panel-body">
                    {% for project in project_dict.my %}
                        <div class="item">
                            <a href="{% url 'web:dashboard' project.value.id %}" class="title"
                               style="background-color: {{ project.value.get_color_display }}"> {{ project.value.name }}
                            </a>
                            <div class="info">
                                <div>
                                    <a href="{% url 'web:project_star' project_type=project.type project_id=project.value.id %}">
                                        <i class="fa fa-star" aria-hidden="true" style="color: #d5d5d5;"></i>
                                    </a>
                                    <span>{{ project.value.creator.username }}</span>
                                </div>
                                <div>
                                    <i class="fa fa-user-o" aria-hidden="true"></i>
                                    <span>{{ project.value.join_count }}</span>
                                </div>

                            </div>
                        </div>
                    {% endfor %}
                </div>
            </div>
        {% endif %}
        {#  我参与的项目  #}
        {% if project_dict.join %}
            <div class="panel panel-default">
                <div class="panel-heading"><i class="fa fa-handshake-o" aria-hidden="true"></i>&nbsp; 我参与的</div>
                <div class="panel-body">
                    {% for project in project_dict.join %}
                        <div class="item">
                            <a href="{% url 'web:dashboard' project.value.id %}" class="title"
                               style="background-color: {{ project.value.get_color_display }}"> {{ project.value.name }}
                            </a>
                            <div class="info">
                                <div>
                                    <a href="{% url 'web:project_star' project_type=project.type project_id=project.value.id %}">
                                        <i class="fa fa-star" aria-hidden="true" style="color: #d5d5d5;"></i>
                                    </a>
                                    <span>{{ project.value.creator.username }}</span>
                                </div>
                                <div>
                                    <i class="fa fa-user-o" aria-hidden="true"></i>
                                    <span>{{ project.value.join_count }}</span>
                                </div>

                            </div>
                        </div>
                    {% endfor %}
                </div>
            </div>
            </div>
        {% endif %}



    {#  新建项目弹框  #}
    <!-- Modal -->
    <div class="modal fade" id="addProjectModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新建项目</h4>
                </div>
                <div class="modal-body">
                    <form id="addProjectForm">
                        {% csrf_token %}
                        {% for field in form %}
                            <div class="form-group">
                                <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>

                        {% endfor %}


                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                    <button id="btnSubmit" type="button" class="btn btn-primary">确 定</button>
                </div>
            </div>
        </div>
    </div>


{% endblock %}

{% block js %}
    <script>
        $(function () {
            bindClickSubmit();
        });

        /*
       * 点击提交（新建项目）
       * */
        function bindClickSubmit() {
            $('#btnSubmit').click(function () {
                // 点击前，将所有报错清空
                $('.error-msg').empty();

                // 数据ajax发送到后台
                $.ajax({
                    url: "{% url 'web:project_list' %}",
                    type: 'POST',
                    // 收集表单中的数据（找到每一个字段）
                    data: $('#addProjectForm').serialize(),  // serialize携带所有字段的数据、以及csrf的token
                    dataType: "JSON", //将服务器返回的数据反序列化为字典你
                    success: function (res) {

                        // 新建项目成功
                        if (res.status) {
                            // ajax请求发送成功之后，自动执行的函数：res就是后端返回的值
                            console.log("新建项目成功")
                            // 刷新页面
                            location.reload()
                        } else {
                            console.log(res)
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0])
                            })
                        }
                    }
                })
            })

        }
    </script>



{% endblock %}